import server from '@/utils/server'
import ServerConstants from '@/utils/ServerConstants'


/*获取商品分类*/
export function goodsCategoryList (params, success, errorfun) {
  if (server._isEmpty(params)) {
    params = {}
  }

  return new Promise((resolve, reject) => {
    server.getJSON(ServerConstants.REQ_GET_GOODS_CATAGORY_LIST, params,
      function (response) {
        resolve(response.data)
        server.call(success, response)
      }, function (error) {
        server.call(errorfun, error)
        reject(error)
      })
  })
}





/*获取商品列表*/
export function getGoodsList (params, success, errorfun) {
  if (server._isEmpty(params)) {
    params = {}
  }

  return new Promise((resolve, reject) => {
    server.getJSON(ServerConstants.REQ_GET_GOODS_LIST, params,
      function (response) {
        resolve(response.data)
        server.call(success, response)
      }, function (error) {
        server.call(errorfun, error)
        reject(error)
      })
  })
}

/*获取最新的商品*/
export function getGoodsListNew (params, success, errorfun) {
  if (server._isEmpty(params)) {
    params = {}
  }
  return new Promise((resolve, reject) => {
    server.getJSON(ServerConstants.REQ_GET_GOODS_LIST_NEW, params,
      function (response) {
        resolve(response.data)
        server.call(success, response)
      }, function (error) {
        server.call(errorfun, error)
        reject(error)
      })
  })
}

/*获取推荐的商品*/
export function getGoodsListRecommand (params, success, errorfun) {
  if (server._isEmpty(params)) {
    params = {}
  }
  return new Promise((resolve, reject) => {
    server.getJSON(ServerConstants.REQ_GET_GOODS_LIST_RECOMAND, params,
      function (response) {
        resolve(response.data)
        server.call(success, response)
      }, function (error) {
        server.call(errorfun, error)
        reject(error)
      })
  })
}



export function searchGoods (params, success, errorfun) {
  if (server._isEmpty(params)) {
    params = {}
  }
  return new Promise((resolve, reject) => {
    server.getJSON(ServerConstants.REQ_GET_GOODS_LIST_SEARCH, params,
      function (response) {
        resolve(response.data)
        server.call(success, response)
      }, function (error) {
        server.call(errorfun, error)
        reject(error)
      })
  })
}

export function goodsInfo (params, success, errorfun) {
  if (server._isEmpty(params)) {
    params = {}
  }
  return new Promise((resolve, reject) => {
    server.getJSON(ServerConstants.REQ_GET_GOODS_INFO, params,
      function (response) {
        resolve(response.data)
        server.call(success, response)
      }, function (error) {
        server.call(errorfun, error)
        reject(error)
      })
  })
}
